package com.leyou.item.controller;

import com.github.pagehelper.PageInfo;
import com.leyou.common.entity.MyPage;
import com.leyou.common.entity.MyPage1;
import com.leyou.item.entity.TbSku;
import com.leyou.item.entity.TbSpu;
import com.leyou.item.entity.TbSpuBo;
import com.leyou.item.entity.TbSpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //查询
    @GetMapping("/spu/page")
    public ResponseEntity<MyPage<TbSpuBo>> querySpuByKey(
            String key,
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "5")int rows,
            Boolean saleable
    ){
        PageInfo<TbSpuBo> tbSpuBoPageInfo = goodsService.querySpuBosByKey(key, page, rows, saleable);
        if(CollectionUtils.isEmpty(tbSpuBoPageInfo.getList())){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        MyPage<TbSpuBo> tbSpuBoMyPage = new MyPage<>();
        tbSpuBoMyPage.setItems(tbSpuBoPageInfo.getList());
        tbSpuBoMyPage.setTotal(tbSpuBoPageInfo.getTotal());
        return ResponseEntity.ok(tbSpuBoMyPage);
    }



    //查询spu信息
    @GetMapping("/spu/page1")
    public ResponseEntity<MyPage1<TbSpuBo>> querySpuByKey1(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "5")int rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        PageInfo<TbSpuBo> tbSpuBoPageInfo = goodsService.querySpuBosByKey(key, page, rows, saleable);

        MyPage1<TbSpuBo> tbSpuBoMyPage = new MyPage1<>();
        tbSpuBoMyPage.setItems(tbSpuBoPageInfo.getList());
        tbSpuBoMyPage.setTotal(tbSpuBoPageInfo.getTotal());
        return ResponseEntity.ok(tbSpuBoMyPage);
    };


    //添加
    @PostMapping("goods")
    public ResponseEntity<Void> addGoods(@RequestBody TbSpuBo spuBo){
        try {
            goodsService.addGoods(spuBo);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }

    //修改
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody TbSpuBo spuBo){
        try {
            goodsService.updateGoods(spuBo);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }


    //删除商品====改变valid属性值
    @DeleteMapping("goods/delete")
    public ResponseEntity<Void> deleteGoods(Long id){
        try {
            goodsService.deleteGoods(id);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);//期望失败 417
        }
    }
    //根据spuId获取spu详细信息
    @GetMapping("spu/detail/{id}")
    public ResponseEntity<TbSpuDetail> querySpuDetailById(@PathVariable("id") Long id){
        TbSpuDetail tbSpuDetail = goodsService.querySpuDetailById(id);
        if(tbSpuDetail==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(tbSpuDetail);
    }

    //根据spu查询sku集合
    @GetMapping("sku/list")
    public ResponseEntity<List<TbSku>> querySkusBySpuId(Long id){
        List<TbSku> spus=goodsService.querySkusBySpuId(id);
        if(spus==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spus);
    }

    //商品上下架
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> saleableGoods(@RequestBody TbSpu spu){
        try {
            goodsService.saleableGoods(spu);
            return ResponseEntity.ok().build();
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.EXPECTATION_FAILED);
        }

    }

    //根据主键id获取spu
    @GetMapping("spu/{id}")
    public ResponseEntity<TbSpu> querySpuById(@PathVariable("id") Long id){
        TbSpu tbSpu = goodsService.querySpuById(id);
        if(tbSpu==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(tbSpu);
    }
    


   /* @GetMapping("spu/detail/{id}")
    public ResponseEntity<TbSpuBo> querySpuById(@PathVariable("id") Long id){
        TbSpuBo tbSpuBo = goodsService.querySpuById(id);
        if(tbSpuBo!=null){

            return ResponseEntity.ok(tbSpuBo);
        }else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
*/

}
